package com.powernode.dataaccessobject;

import com.powernode.commons.util.DbUtils;
import com.powernode.dataaccessobject.dao.EmployeeDao;
import com.powernode.dataaccessobject.entity.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * @Author Arrebol
 * @Date 2024/8/3 11:24
 * @Project jdbc
 * @Description：
 */
public class JdbcDaoTest1 {
    public static Connection conn;
    public static PreparedStatement ps;
    public static ResultSet employee;
    public static Scanner sc;

    static {
        try {
            sc = new Scanner(System.in);
            conn = DbUtils.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        //欢迎界面
        welcome();
        while (true) {
            System.out.println("请输入功能编号：");
            int order = sc.nextInt();
            switch (order) {
                case 1:
                    listQueryEmployee();
                    break;
                case 2:
                    queryByEmployeeId();
                    break;
                case 3:
                    addEmployee();
                    break;
                case 4:
                    updateEmployee();
                    break;
                case 5:
                    deleteByEmployeeId();
                    break;
                case 0:
                    exit();
                    break;
                default:
                    System.out.println("输入的指令有误，请仔细阅读系统使用说明书");
                    break;
            }
        }
    }

    /**
     * 根据id删除员工信息
     *
     * @throws SQLException
     */
    private static void deleteByEmployeeId() throws SQLException {
        EmployeeDao employeeDao = new EmployeeDao();

        System.out.println("删除员工");
        System.out.println("请输入要删除员工的id");
        long id = sc.nextLong();
        int count = employeeDao.deleteByEmployeeId(id);
        System.out.println(count == 0 ? "删除员工失败" : "删除员工成功");
    }

    /**
     * 退出系统
     */
    private static void exit() {
        System.out.println("退出系统");
        DbUtils.close(employee, ps, conn);
        System.exit(1);
    }

    /**
     * 根据id更改员工信息
     *
     * @throws SQLException
     */
    private static void updateEmployee() throws SQLException {
        EmployeeDao employeeDao = new EmployeeDao();
        System.out.println("修改员工");
        System.out.println("请输入要修改的员工的id：");
        long id = sc.nextLong();
        System.out.println("请输入要修改此员工的信息：");
        System.out.println("姓名：");
        String name = sc.next();
        System.out.println("职位：");
        String job = sc.next();
        System.out.println("入职日期：");
        String hireDate = sc.next();
        System.out.println("工资");
        double salary = sc.nextDouble();
        System.out.println("地址：");
        String address = sc.next();
        Employee newEmployee = new Employee(id, name, job, salary, hireDate, address);
        int count = employeeDao.update(newEmployee);
        System.out.println(count == 0 ? "修改员工失败" : "修改员工成功");
    }

    /**
     * 新增员工
     *
     * @throws SQLException
     */
    private static void addEmployee() throws SQLException {
        EmployeeDao employeeDao = new EmployeeDao();
        System.out.println("新增员工");
        System.out.println("请输入新增员工的相关信息：");
        System.out.println("姓名：");
        String name = sc.next();
        System.out.println("职位：");
        String job = sc.next();
        System.out.println("入职日期：");
        String hireDate = sc.next();
        System.out.println("工资");
        double salary = sc.nextDouble();
        System.out.println("地址：");
        String address = sc.next();
        Employee newEmployee = new Employee();
        newEmployee.setName(name);
        newEmployee.setAddress(address);
        newEmployee.setJob(job);
        newEmployee.setSalary(salary);
        newEmployee.setHireDate(hireDate);
        int count = employeeDao.insert(newEmployee);
        System.out.println(count == 0 ? "新增员工失败" : "新增员工成功");
    }

    /**
     * 根据员工id查询员工信息
     *
     * @throws SQLException 可能出现的运行时异常
     */
    private static void queryByEmployeeId() throws SQLException {
        EmployeeDao employeeDao = new EmployeeDao();
        System.out.println("根据员工id查询员工详细信息");
        System.out.println("请输入要查询的员工id");
        long id = sc.nextLong();
        Employee employee = employeeDao.selectById(id);
        if (employee != null) {
            System.out.println("查询到的员工信息如下：");
            System.out.println("id：" + employee.getId());
            System.out.println("name：" + employee.getName());
            System.out.println("job：" + employee.getJob());
            System.out.println("hireDate：" + employee.getHireDate());
            System.out.println("salary：" + employee.getSalary());
            System.out.println("address：" + employee.getAddress());
        }
    }

    /**
     * 查询员工列表
     *
     * @throws SQLException 可能出现的运行时异常
     */
    private static void listQueryEmployee() throws SQLException {
        EmployeeDao employeeDao = new EmployeeDao();
        List<Employee> employees = employeeDao.selectAll();
        System.out.println("姓名\t职位");
        employees.forEach(e -> System.out.println(e.getName() + "\t" + e.getJob()));
    }

    /**
     * 欢迎界面
     */
    private static void welcome() {
        System.out.println("欢迎使用员工信息管理系统，请认真阅读使用说明。");
        System.out.println("本系统的主要功能如下：查看员工列表，查看员工详细信息，新增员工，修改员工，删除员工");
        System.out.println("请输入对应的功能编号以选择功能");
        System.out.println("1：查看员工列表");
        System.out.println("2：查看员工详细信息");
        System.out.println("3：新增员工");
        System.out.println("4：修改员工");
        System.out.println("5：删除员工");
        System.out.println("0：退出系统");
    }
}
